package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.result.Result;
import com.ruoyi.system.domain.Clazz;
import io.swagger.annotations.Api;
import com.ruoyi.system.service.IClassService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static org.apache.naming.SelectorContext.prefix;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author FSD
 * @since 2024-02-18
 */
@Api(tags = "班级表")
@RestController
@RequestMapping("/api/system/class")
public class ClazzController {
    private String prefix = "/api/system/class";
    @Resource(name = "ClassServiceImpl")
    private IClassService classService;


    @RequiresPermissions("system:clazz:view")
    @GetMapping()
    public String clazz()
    {
        return prefix + "/clazz";
    }
    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增班级", notes = "新增班级")
    @ApiImplicitParam(name = "clazz", value = "班级实体类",dataType = "String",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    @Log(title = "班级表", businessType = BusinessType.INSERT)
    public Result insert(@RequestBody Clazz clazz) {
        return Result.ok(classService.save(clazz));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改班级", notes = "修改班级")
    @ApiImplicitParam(name = "clazz", value = "班级实体类",dataType = "String",dataTypeClass = String.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result update(@RequestBody Clazz clazz) {
        return Result.ok(classService.updateById(clazz));
    }

    /**
     * 根据id删除
     */
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除班级", notes = "根据id删除班级")
    @ApiImplicitParam(name = "id", value = "id",dataType = "Integer",dataTypeClass = Integer.class)
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result delete(@PathVariable Integer id) {
        return Result.ok(classService.removeById(id));
    }



    /**
     * 查询全部
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询全部班级", notes = "查询全部班级")
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result findAll() {
        return Result.ok(classService.list());
    }


    /**
     * 分页模糊查询
     */
    @GetMapping("/get/page")
    @ApiOperation(value = "分页模糊查询班级", notes = "分页模糊查询班级")
    @RequiresRoles(value = {"admin", "teacher"}, logical = Logical.OR)
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        // 倒序查询
        queryWrapper.orderByDesc("id");
        return Result.ok(classService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
}

